//
//  ChatListCell.m
//  ChatForHot
//
//  Created by FLY on 2018/10/16.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "ChatListCell.h"

@implementation ChatListCell



#pragma mark - lyout views

- (instancetype)initWithStyle:(UITableViewCellStyle)style
              reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //        _showAvatar = YES;
        [self layoutViews];
    }
    
    return self;
}

/*!
 @method
 @brief 加载视图
 //@discussion
 //@ return
 */
- (void)layoutViews
{
    self.accessibilityIdentifier = @"table_cell";
    
    [self.contentView addSubview:self.HOTCHATheadimgView];
    [self.contentView addSubview:self.HOTCHATtimeLabel];
    [self.contentView addSubview:self.HOTCHATtitleLabel];
    [self.contentView addSubview:self.HOTCHATdetailLabel];
    
    [self.contentView addSubview:self.HOTCHATchattypeImgView];
    [self.contentView addSubview:self.HOTCHATchattypeLab];
    
    //my
    [self.contentView addSubview:self.HOTCHATlineView];
    [self addSubview:self.HOTCHATvipImgView];
    [self addSubview:self.HOTCHATnumImgView];
    [self addSubview:self.HOTCHATnumLab];
    
    [self.HOTCHATheadimgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.width.height.mas_equalTo(50);
        make.centerY.equalTo(self);
    }];
    [self.HOTCHATvipImgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(20);
        make.bottom.right.equalTo(self.HOTCHATheadimgView);
    }];
    [self.HOTCHATtitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.height.mas_equalTo(24);
        make.left.equalTo(self.HOTCHATheadimgView.mas_right).offset(15);
        //        make.right.lessThanOrEqualTo(self.chattypeImg.mas_left).offset(-5);
    }];
    [self.HOTCHATchattypeImgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.HOTCHATtitleLabel.mas_right).offset(15);
        make.height.mas_equalTo(16);
        make.centerY.equalTo(self.HOTCHATtitleLabel);
        make.right.equalTo(self.HOTCHATchattypeLab).offset(5);
        make.right.lessThanOrEqualTo(self.HOTCHATtimeLabel.mas_left).offset(-15);
    }];
    [self.HOTCHATchattypeLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.HOTCHATchattypeImgView.mas_left).offset(16);
        make.top.bottom.equalTo(self.HOTCHATchattypeImgView);
        make.centerY.equalTo(self.HOTCHATtitleLabel);
    }];
    [self.HOTCHATdetailLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.HOTCHATtitleLabel);
        make.height.mas_equalTo(22);
        make.bottom.equalTo(self).offset(-10);
        make.right.lessThanOrEqualTo(self.HOTCHATnumImgView.mas_left).offset(-15);
    }];
    [self.HOTCHATtimeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-15);
        make.height.mas_equalTo(15);
        make.centerY.equalTo(self.HOTCHATtitleLabel);
        //        make.width.mas_greaterThanOrEqualTo(80);
    }];
    [self.HOTCHATnumImgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.HOTCHATnumLab);
        make.left.equalTo(self.HOTCHATnumLab).offset(-7);
        make.right.equalTo(self.HOTCHATnumLab).offset(7);
    }];
    [self.HOTCHATnumLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-22);
        make.height.mas_equalTo(15);
        make.centerY.equalTo(self.HOTCHATdetailLabel);
    }];
    [self.HOTCHATlineView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    
    //    [self.titleLabel setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    [self.HOTCHATtitleLabel setContentCompressionResistancePriority:(UILayoutPriorityDefaultLow) forAxis:UILayoutConstraintAxisHorizontal];
    [self.HOTCHATdetailLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    [self.HOTCHATnumImgView setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    [self.HOTCHATnumLab setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
}

- (void)setBadgeNum:(NSInteger)badgeNum{
    _badgeNum = badgeNum;
    if (badgeNum == 0) {
        self.HOTCHATnumLab.hidden = YES;
        self.HOTCHATnumImgView.hidden = YES;
    }else{
        self.HOTCHATnumLab.hidden = NO;
        self.HOTCHATnumImgView.hidden = NO;
        if (badgeNum > 99) {
            self.HOTCHATnumLab.text = @"99+";
        }else{
            self.HOTCHATnumLab.text = [NSString stringWithFormat:@"%ld",(long)badgeNum];
        }
    }
}
- (void)setVipImg:(NSString *)vipImg{
    _vipImg = vipImg;
    if (vipImg) {
        self.HOTCHATvipImgView.image = [UIImage imageNamed:vipImg];
    }else{
        self.HOTCHATvipImgView.image = nil;
    }
    
}


//- (void)setChatType:(NSInteger)chatType{
//    _chatType = chatType;
//    
//    UIImage *image = [UIImage chatImageWithType:chatType];
//    if (image) {
//        self.HOTCHATchattypeImgView.image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(1, 15, 1, 10) resizingMode:(UIImageResizingModeStretch)];
//        self.HOTCHATchattypeLab.text = [NSString ONENIGHTchatTypeWithType:chatType];
//    }else{
//        self.HOTCHATchattypeImgView.image = nil;
//        self.HOTCHATchattypeLab.text = nil;
//    }
//}
//lazzy
- (UIImageView *)HOTCHATchattypeImgView{
    if (_HOTCHATchattypeImgView == nil) {
        _HOTCHATchattypeImgView = [[UIImageView alloc] init];
    }
    return _HOTCHATchattypeImgView;
}
- (UILabel *)HOTCHATchattypeLab{
    if (_HOTCHATchattypeLab == nil) {
        _HOTCHATchattypeLab = [[UILabel alloc] init];
        _HOTCHATchattypeLab.font = [UIFont systemFontOfSize:8];
        _HOTCHATchattypeLab.textColor = [UIColor whiteColor];
    }
    return _HOTCHATchattypeLab;
}

- (UIImageView *)HOTCHATheadimgView{
    if (_HOTCHATheadimgView == nil) {
        _HOTCHATheadimgView = [[UIImageView alloc] init];
        _HOTCHATheadimgView.layer.cornerRadius = 25;
        _HOTCHATheadimgView.clipsToBounds = YES;
        _HOTCHATheadimgView.backgroundColor = [UIColor lightGrayColor];
        _HOTCHATheadimgView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _HOTCHATheadimgView;
}
- (UIImageView *)HOTCHATvipImgView{
    if (_HOTCHATvipImgView == nil) {
        _HOTCHATvipImgView = [[UIImageView alloc] init];
//        _HOTCHATvipImgView.image = [[UIImage imageNamed:[ONENIGHTStringUtils getThemsg_vip]] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 8, 1, 8) resizingMode:UIImageResizingModeStretch];
    }
    return _HOTCHATvipImgView;
}
- (UILabel *)HOTCHATtitleLabel{
    if (_HOTCHATtitleLabel == nil) {
        _HOTCHATtitleLabel = [[UILabel alloc] init];
        _HOTCHATtitleLabel.font = [UIFont systemFontOfSize:14];
        _HOTCHATtitleLabel.textColor = HOTCHATrgb(26, 26, 26);
    }
    return _HOTCHATtitleLabel;
}
- (UILabel *)HOTCHATdetailLabel{
    if (_HOTCHATdetailLabel == nil) {
        _HOTCHATdetailLabel = [[UILabel alloc] init];
        _HOTCHATdetailLabel.font = [UIFont systemFontOfSize:12];
        _HOTCHATdetailLabel.textColor = HOTCHATrgb(128, 128, 128);
    }
    return _HOTCHATdetailLabel;
}
- (UILabel *)HOTCHATtimeLabel{
    if (_HOTCHATtimeLabel == nil) {
        _HOTCHATtimeLabel = [[UILabel alloc] init];
        _HOTCHATtimeLabel.font = [UIFont systemFontOfSize:10];
        _HOTCHATtimeLabel.textColor = HOTCHATrgb(128, 128, 128);
        _HOTCHATtimeLabel.textAlignment = NSTextAlignmentRight;
    }
    return _HOTCHATtimeLabel;
}
- (UIImageView *)HOTCHATnumImgView{
    if (_HOTCHATnumImgView == nil) {
        _HOTCHATnumImgView = [[UIImageView alloc] init];
        _HOTCHATnumImgView.image = [[[UIImage imageNamed:@"msg_badge"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 7, 1, 7) resizingMode:(UIImageResizingModeStretch)] imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
        _HOTCHATnumImgView.tintColor = HOTCHATMainColor;
    }
    return _HOTCHATnumImgView;
}
- (UILabel *)HOTCHATnumLab{
    if (_HOTCHATnumLab == nil) {
        _HOTCHATnumLab = [[UILabel alloc] init];
        _HOTCHATnumLab.font = [UIFont systemFontOfSize:8];
        _HOTCHATnumLab.textColor = [UIColor whiteColor];
        _HOTCHATnumLab.text = @"1";
        _HOTCHATnumLab.textAlignment = NSTextAlignmentRight;
    }
    return _HOTCHATnumLab;
}
- (UIView *)HOTCHATlineView{
    if (_HOTCHATlineView == nil) {
        _HOTCHATlineView = [[UIView alloc] init];
        _HOTCHATlineView.backgroundColor = HOTCHATrgb(229, 229, 229);
    }
    return _HOTCHATlineView;
}
- (void)setChatType:(NSInteger)chatType{
    _chatType = chatType;
    
    UIImage *image = [UIImage HOTCHATchatImageForType:chatType];
    if (image) {
        self.HOTCHATchattypeImgView.image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(1, 15, 1, 10) resizingMode:(UIImageResizingModeStretch)];
        self.HOTCHATchattypeLab.text = [NSString HOTCHATchatTypeForType:chatType];
    }else{
        self.HOTCHATchattypeImgView.image = nil;
        self.HOTCHATchattypeLab.text = nil;
    }
}
@end
